package com.chery.media.utils

import android.content.Intent
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.AppUtils
import com.chery.media.contacts.StorageContacts
import com.chery.media.library.extend.getStorageInt
import com.chery.media.library.extend.storageData
import com.chery.media.ui.main.MainActivity
import com.mallotec.reb.localeplugin.utils.ActivityHelper
import com.mallotec.reb.localeplugin.utils.LocaleHelper
import java.util.Locale

/**
 * 语言切换
 */
object LanguageSwitchUtils {

    /**
     *  获取当前语言
     */
    fun getCurrentLanguage(): Int {
        return getStorageInt(StorageContacts.CURRENT_LANGUAGE) ?:0
    }

    /**
     *  切换语言
     */
    fun switchLanguage(language: Int = 0) {
        storageData(StorageContacts.CURRENT_LANGUAGE, language)
        // 应用切换的语言
        val intent = Intent(ActivityUtils.getTopActivity(), MainActivity::class.java)
        LocaleHelper.getInstance()
            .language(getLocale(2))
            .apply(ActivityUtils.getTopActivity())
        val locale = getLocale(language)
        LocaleHelper.getInstance()
            .language(locale)
//            .apply(ActivityUtils.getTopActivity())
            .apply(ActivityUtils.getTopActivity(), intent)
//            .apply(ActivityUtils.getTopActivity(), intent,  ActivityHelper.getInstance())
//        LiveEventBus.get<Unit>(EventContacts.SWITCH_LANGUAGE).post(Unit)
//        AppUtils.relaunchApp(true)
    }

    /**
     *  获取语言
     */
    private fun getLocale(switch : Int): Locale {
        return when (switch) {
            1 -> Locale("en")  // 英语
            2 -> Locale.SIMPLIFIED_CHINESE // 中文
            3 -> Locale("ru") // 俄语
            4 -> Locale("uz") //乌兹别克语
            else -> Locale.SIMPLIFIED_CHINESE
        }
    }

}